package com.youlu.campus.service.data.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.VO.req.UserQaMqMessageReq;
import com.youlu.campus.service.cache.CacheActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class UserQaMQService {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private CacheActivityService cacheActivityService;

    @Value("${mq.topic.userQa}")
    private String userQaTopic;

    @Value("${mq.topic.userBaoMing}")
    private String userBaoMing;

    @Async
    public void sendUserQa(UserQaMqMessageReq userQaMqMessageReq){
        log.info("活动 - > {} 统计- start userId - > {} 答题",userQaMqMessageReq.getActivityId(), userQaMqMessageReq.getUserName());
        ActivityInfo activityInfo = cacheActivityService.findById(userQaMqMessageReq.getActivityId());
        userQaMqMessageReq.setActivityName(activityInfo.getName());
        rocketMQTemplate.asyncSend(userQaTopic+":QA", JSON.toJSONString(userQaMqMessageReq), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
            }
            @Override
            public void onException(Throwable throwable) {
                log.error("QA  ->{} 消息失败",throwable);
            }
        });
    }

    @Async
    public void sendUserBaoMing(UserQaMqMessageReq userQaMqMessageReq){
        log.info("活动 - > {} 统计- start userId - > {} 报名",userQaMqMessageReq.getActivityId(), userQaMqMessageReq.getUserName());
        ActivityInfo activityInfo = cacheActivityService.findById(userQaMqMessageReq.getActivityId());
        userQaMqMessageReq.setActivityName(activityInfo.getName());
        rocketMQTemplate.asyncSend(userBaoMing+":BAOMING", JSON.toJSONString(userQaMqMessageReq), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
            }
            @Override
            public void onException(Throwable throwable) {
                log.error("报名失败  ->{} 消息失败",throwable);
            }
        });
    }






}
